package cn.eng.org.service.impl;

import cn.eng.basic.commonenum.ErrorCode;
import cn.eng.basic.constants.Constant;
import cn.eng.basic.domain.dto.ShopDto;
import cn.eng.basic.exception.BusinessException;
import cn.eng.basic.utils.BaiduAuditUtils;
import cn.eng.org.domain.Employee;
import cn.eng.org.domain.Shop;
import cn.eng.org.domain.ShopAuditLog;
import cn.eng.org.mapper.EmployeeMapper;
import cn.eng.org.mapper.ShopAuditLogMapper;
import cn.eng.org.mapper.ShopMapper;
import cn.eng.org.service.IShopService;
import cn.eng.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
@SuppressWarnings("all")
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     *店铺入驻
     * @param shop
     */
    @Override
    @Transactional
    public void settlement(Shop shop) {
        //1.空值校验
        //2.校验两次密码是否一致
        if (!shop.getAdmin().getPassword().equals(shop.getAdmin().getComfirmPassword())){
            throw new BusinessException(ErrorCode.PASSWORD_ERROR.getCode());
        }
        //3.判断店铺是否已注册
        Shop shopTmp  = shopMapper.loadByNameOrAddress(shop);
        if (shopTmp !=null){
            throw new BusinessException(ErrorCode.SHOP_ERROR.getCode());
        }
        //百度文本审核
       if (!BaiduAuditUtils.TextCensor(shop.getName())){
           throw new BusinessException(ErrorCode.BAIDUAI_TEXT.getCode());
       }
       //百度图片智能审核
       if (shop.getLogo() !=null){
           if (!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
               throw new BusinessException(ErrorCode.BAIDUAI_IMG.getCode());
           }
       }
        //4.添加店铺
        shop.setState(0);
        shop.setRegisterTime(new Date());
        shopMapper.save(shop);
        //获取管理员
        Employee employee = employeeMapper.loadByUsername(shop.getAdmin().getUsername());
        if (employee !=null){
            employee.setShop_id(shop.getId());
            shop.setAdminId(shop.getAdmin().getId());
        }else {
             //获取员工对象
             employee = shop.getAdmin();
             //添加店铺id
             employee.setShop_id(shop.getId());
             //设置状态
            employee.setState(1);
            employeeMapper.save(employee);
            //给店铺绑定店长
            shop.setAdminId(shop.getAdmin().getId());
            //更新shop数据
            shopMapper.update(shop);
        }


    }

    /**
     * 审核 - 通过
     * @param log
     */
    @Override
    @Transactional
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.修改状态 0待审核-> 1待激活
        shopMapper.updateState(log.getShopId(),1);
        //2.添加日志
        log.setState(1);
        log.setAuditTime(new Date());
        shopAuditLogMapper.save(log);
        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2553663780@qq.com");
        helper.setSubject("店铺邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()
                        +"' ><a href='http://127.0.0.1:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",
                true);
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);

    }

    /**
     * 审核 - 驳回
     * @param log
     */
    @Override
    @Transactional
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.修改状态 0待审核-> 1待激活
        shopMapper.updateState(log.getShopId(),-1);
        //2.添加日志
        log.setState(-1);
        log.setAuditTime(new Date());
        shopAuditLogMapper.save(log);
        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2553663780@qq.com");
        helper.setSubject("店铺审核失败");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.loadById(log.getShopId());
        helper.setText("<h1>你的店铺入驻失败，请修改信息之后重新入驻</h1>!!!",
                true);
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    /**
     * 柱状图的数据
     * @return
     */
    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }
}
